import requests
import time
import csv
import math
import time
# 处理年历和月历
import calendar
# 截至到2021年1月23日的数据
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36'
}
lists = ['year', 'mon', 'week', 'day']
for i in lists:
    url = f'https://api.huobipro.com/market/history/kline?period=1{i}&size=2000&symbol=btcusdt&AccessKeyId=3290265c-208217e4-9f4199c1-mk0lklo0de'
    print(url)
# 获取全部年的数据
# urlyear = 'https://api.huobipro.com/market/history/kline?period=1year&size=2000&symbol=btcusdt&AccessKeyId=3290265c-208217e4-9f4199c1-mk0lklo0de'
# # 获取全部月的数据
# urlmon = 'https://api.huobipro.com/market/history/kline?period=1mon&size=2000&symbol=btcusdt&AccessKeyId=3290265c-208217e4-9f4199c1-mk0lklo0de'
# # 获取全部周的数据
# urlweek = 'https://api.huobipro.com/market/history/kline?period=1week&size=2000&symbol=btcusdt&AccessKeyId=3290265c-208217e4-9f4199c1-mk0lklo0de'
# # 获取全部天的数据
# urlday = 'https://api.huobipro.com/market/history/kline?period=1day&size=2000&symbol=btcusdt&AccessKeyId=3290265c-208217e4-9f4199c1-mk0lklo0de'
# # 获取全部小时的数据
# urlhour = 'https://api.huobipro.com/market/history/kline?period=60min&size=2000&symbol=btcusdt&AccessKeyId=3290265c-208217e4-9f4199c1-mk0lklo0de'
# response = requests.get(urlyear, headers=headers)
# response = requests.get(urlmon, headers=headers)
# response = requests.get(urlweek, headers=headers)
    response = requests.get(url, headers=headers)
# response = requests.get(urlhour, headers=headers)
    print(response.status_code)
# print(response.json())
    if response.json().get("status") == "ok":
        datas = response.json().get("data")
        # 输出data全部的数据
        # print(datas)
        #界面我们再取每条数据

        # with open(r"C:\Users\PC\Desktop\火币\比特币\年统计.csv", 'w', encoding='GB2312', newline="") as f:
        # with open(r"C:\Users\PC\Desktop\火币\比特币\月统计.csv", 'w', encoding='GB2312', newline="") as f:
        # with open(r"C:\Users\PC\Desktop\火币\比特币\周统计.csv", 'w', encoding='GB2312', newline="") as f:
        with open(f"C:/Users/PC/Desktop/火币/比特币/{i}.csv", 'w', encoding='GB2312', newline="") as f:
            writer = csv.writer(f)
            writer.writerow(['k线id', '成交量', '成交笔数', '开盘价', '收盘价', '最低价', '最高价', '成交额', '涨幅(24H)', '成交时间'])
            for data in datas:
                print(data)
                # 加入日期(定义变量加入日期)
                # 加入时间戳,将时间戳转化为具体时间
                # 找到其中的时间戳转为具体时间
                # k线的id
                id = data['id']
                # timestamp = int(id)
                # 利用localtime()函数将时间戳转化成localtime的格式
                time_local = time.localtime(id)
                # 利用strftime()函数重新格式化时间
                dt = time.strftime("%Y-%m-%d %H:%M", time_local)
                print(dt)
                # 成交量
                amount = data['amount']
                # 成交笔数
                count = data['count']
                # 开盘价
                open = data['open']
                # 收盘价
                close = data['close']
                #最低价
                low = data['low']
                # 最高价
                high = data['high']
                # 成交额
                vol = data['vol']
                # 增加了涨幅计算
                # 涨幅情况(计算形式: (收盘价-开盘价)/开盘价*100)=涨幅度)
                increase = '{:.2f} %'.format((close - open)/open*100)
                print(increase)

                writer.writerow([id, amount, count, open, close, low, high, vol, increase, dt])
    f.close()
